package com.lvtongka.app.ui.qr

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.lvtongka.app.base.BaseActivity
import com.lvtongka.app.common.AppUtils
import com.lvtongka.app.common.CommonUtils
import com.lvtongka.app.common.ZXingUtils
import com.lvtongka.app.databinding.ActivityQrCodeBinding
import com.lvtongka.app.http.common.WebConstants
import com.lvtongka.app.ui.coupon.bean.CouponItem
import com.lvtongka.app.ui.webview.WebViewActivity

/**
 * describe:
 * 二维码界面
 * @author:ZhangWei(张伟)
 * date:2024/12/04
 * time:19:58
 */
class QrCodeActivity : BaseActivity<ActivityQrCodeBinding, PaymentCodeViewModel>(){
    private var couponItem: CouponItem? = null
    private var dynCode: String = ""
    private var isFinish = false
    override fun initViewModel(): PaymentCodeViewModel {
        return ViewModelProvider(this)[PaymentCodeViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        isBlack = false
        super.onCreate(savedInstanceState)
    }

    override fun initEvent() {
        binding.imgBack.setOnClickListener { finish() }
        binding.llOffline.setOnClickListener {
            if (isFastClick()) {
                return@setOnClickListener
            }
            // 跳转到webView
            jumpWeb(WebConstants.QUAN_USABLE + couponItem!!.typeId)
        }

        viewModel.error2.observe(this) {
            hideLoading()
            showToast(it)
        }

        viewModel.qrCodeData.observe(this) {
            hideLoading()
            it?.let {
                val bitmap2 = ZXingUtils.createQRCode(it.qr_code, AppUtils.dip2px(200F))
                binding.ivQrCode.setImageBitmap(bitmap2)

                dynCode = it.qr_code
                binding.ivQrCode.postDelayed({
                    // 延迟2s查询结果
                    if (!isFinish) {
                        viewModel.queryPayQuanStatus(dynCode)
                    }
                }, 2000)
            }
        }

        viewModel.payQuanStatus.observe(this) {
            if (it.status == "1") {
                // 过期 需要重新请求
                viewModel.generateQRCode(couponItem!!.couponNo)
            } else {
                if (it.order_info != null) {
                    when (it.order_info.pay_result_code) {
                        "0", "1" -> {
                            // 0或1-待用户授权
                            startActivity(Intent(this, PaymentMerchantActivity::class.java).apply {
                                putExtra("qrCode", dynCode)
                                putExtra("order_info", it.order_info)
                                putExtra("isCoupon", true)
                            })
                            setResult(RESULT_OK)
                            finish()
                        }
                        "2" -> {
                            // 2-支付成功
                            startActivity(Intent(this, PaymentCompletedActivity::class.java).apply {
                                putExtra("balance", AppUtils.convertFenToYuan(it.order_info.amt))
                                putExtra("name", it.order_info.shop_name)
                                putExtra("payResult", "支付成功")
                                putExtra("isCoupon", true)
                            })
                            setResult(RESULT_OK)
                            finish()
                        }
                        "3" -> {
                            // 3-支付失败
                            startActivity(Intent(this, PaymentCompletedActivity::class.java).apply {
                                putExtra("balance", AppUtils.convertFenToYuan(it.order_info.amt))
                                putExtra("name", it.order_info.shop_name)
                                putExtra("payResult", "支付失败")
                                putExtra("isCoupon", true)
                            })
                            setResult(RESULT_OK)
                            finish()
                        }
                    }
                } else {
                    binding.ivQrCode.postDelayed({
                        // 延迟2s查询结果
                        if (!isFinish) {
                            viewModel.queryPayQuanStatus(dynCode)
                        }
                    }, 2000)
                }
            }
        }
    }

    override fun initData() {
        binding.tvTitle.text = "券码"
        binding.statusBarView.layoutParams.height = CommonUtils.getStatusBarHeight()

        couponItem = intent.getSerializableExtra("coupon") as CouponItem
        binding.tvCouponName.text = couponItem!!.typeValue
        binding.tvCouponBalance.text = "¥" + AppUtils.convertFenToYuan(couponItem!!.balanceCent)

        showLoading()
        viewModel.generateQRCode(couponItem!!.couponNo)
    }

    override fun initBinding(): ActivityQrCodeBinding {
        return ActivityQrCodeBinding.inflate(layoutInflater)
    }

    override fun onDestroy() {
        isFinish = true
        super.onDestroy()
    }
}